package newcoder.lx_treeMap遍历和排序;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * lx_treeMap遍历和排序
 *
 * @Author Mr.wang
 * @Description
 * @Date 2020-04-13
 */
public class Main {
    public static void main(String[] args) {
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(8, "eight");
        treeMap.put(6, "six");
        treeMap.put(3, "three");
        treeMap.put(12, "twelve");
        treeMap.put(9, "nine");
        treeMap.put(11, "eleven");
        treeMap.put(13, "thirteen");
        treeMap.put(16, "sixteen");
        // 输出Map：{3=three, 6=six, 8=eight, 9=nine, 11=eleven, 12=twelve, 13=thirteen, 16=sixteen}
//        可以看出，treeMap是自动根据key进行升序排序的
        System.out.println("输出Map：" + treeMap);

        Set<Map.Entry<Integer, String>> entries = treeMap.entrySet();
        Iterator<Map.Entry<Integer, String>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            // 得到每一对对应关系
            Map.Entry<Integer, String> next = iterator.next();
            Integer key = next.getKey();
            String value = next.getValue();
            System.out.println("key:" + key + "----------value:" + value);
        }
    }
}
